package com.itheima.consumer.listener;


import cn.hutool.core.bean.BeanUtil;
import com.itheima.consumer.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@Slf4j
@Component
public class myListener {

    @RabbitListener(queues = "hello.q1")
    public void receiveFanout1(String message) {
        log.info("接收到消息：{}", message);
    }

    @RabbitListener(queues = "hello.q2")
    public void receiveFanout2(String message) {
        log.info("接收到消息：{}", message);
    }


    @RabbitListener(queues = "hello.queue")
    public void receiveQ2(String message) {
        log.info("接收到消息2222222：{}", message);
    }

    @RabbitListener(queues = "hello.queue")
    public void receiveQ1(String message) {
        log.info("接收到消息1111111：{}", message);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1", durable = "true"),
            exchange = @Exchange(name = "direct.exchange1", type = ExchangeTypes.DIRECT),
            key = {"red", "blue", "yellow"}
    ))
    public void receiveDirect1(String message) {
        log.info("接收到消息：{}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2", durable = "true"),
            exchange = @Exchange(name = "direct.exchange1", type = ExchangeTypes.DIRECT),
            key = {"blue", "yellow"}
    ))
    public void receiveDirect2(String message) {
        log.info("接收到消息：{}", message);
    }




    @RabbitListener(queues = "object.queuetest")
    public void receiveToMap1(Map<String, Object> message) {
        log.info("接收到消息：{}",message);

        User user = BeanUtil.mapToBean(message, User.class, false);
        throw new RuntimeException("测试异常");

//        log.info("接收到消息：{}", user);
    }


    @RabbitListener(queues = "dlx.queue")
    public void receiveQDLX(Map<String, Object> message) {

//        byte[] body = message.getBody();
//
//        // 转换为字符串
//        String bodyStr = new String(body, StandardCharsets.UTF_8);

        log.info("接收到消息DLX---：{}", message);


    }

}
